package a66weding.com.jiehuntong.activity.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.xutils.x;

import java.math.BigDecimal;
import java.util.List;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.order.RefundActivity;
import a66weding.com.jiehuntong.adapter.MyBaseAdapter;
import a66weding.com.jiehuntong.beans.MyOrderBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;

/**
 * Created by admin on 2016/9/8.
 */
public class OrderDetailsAdapter extends BaseAdapter {
    private Context context;
    private MyOrderBean.DataBean orderDataBean;
    private List<MyOrderBean.DataBean.MyOrderDetailBean> data;
    public OrderDetailsAdapter(Context context,MyOrderBean.DataBean orderDataBean){
        this.context = context;
        this.orderDataBean = orderDataBean;
        this.data = orderDataBean.getMyOrderDetail();
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView==null){
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_order_dingdan,null);
            viewHolder.img_indent = (ImageView) convertView.findViewById(R.id.img_indent);
            viewHolder.tv_indent_code = (TextView) convertView.findViewById(R.id.tv_indent_code);
            viewHolder.tv_me_classify = (TextView) convertView.findViewById(R.id.tv_me_classify);
            viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            viewHolder.orderdetail_name_tv = (TextView) convertView.findViewById(R.id.orderdetail_name_tv);
            viewHolder.orderdetails_state_tv = (TextView) convertView.findViewById(R.id.orderdetails_state_tv);
            viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            viewHolder.tv_me_classify = (TextView) convertView.findViewById(R.id.tv_me_classify);
            viewHolder.acty_refund_progress_group = (RelativeLayout) convertView.findViewById(R.id.acty_refund_progress_group);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        x.image().bind(viewHolder.img_indent, data.get(position).getProductPicMain(), ImageOptionsUtil.getcircle_fixy());
        viewHolder.tv_indent_code.setText(data.get(position).getProductName());
        viewHolder.orderdetail_name_tv.setText(orderDataBean.getBusinessName());
        viewHolder.orderdetails_state_tv.setText(orderDataBean.getOrderStateStr());
        viewHolder.tv_money.setText("¥"+mul(data.get(position).getSinglePrice(),data.get(position).getBuyNumber()));
        viewHolder.tv_me_classify.setText(""+data.get(position).getBuyNumber());
        if (data.get(position).isB_Return()||data.get(position).isB_Exchange()){
            viewHolder.acty_refund_progress_group.setVisibility(View.VISIBLE);
        }else{
            viewHolder.acty_refund_progress_group.setVisibility(View.GONE);
        }
        viewHolder.acty_refund_progress_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, RefundActivity.class);
                MyOrderBean.DataBean.MyOrderDetailBean myOrderDetailBean = data.get(position);
                String ordersNumber = orderDataBean.getOrdersNumber();
                intent.putExtra("OrderDetailsAdapter",myOrderDetailBean);
                intent.putExtra("ordersNumber",ordersNumber);

                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public double mul(double d1,double d2){
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.multiply(bd2).doubleValue();
    }

    public class ViewHolder{
        //图标
        private ImageView img_indent;
        //店铺内容
        private TextView tv_indent_code;
        //数量
        private TextView tv_me_classify;
        //价格
        private TextView tv_money;
        //店铺的名称
        private TextView orderdetail_name_tv;
        //订单状态
        private TextView orderdetails_state_tv;
        //是否退换货
        private RelativeLayout acty_refund_progress_group;
    }
}
